//
//  DLCircleListHeaderView.m
//  FRP_OC
//
//  Created by DeLongYang on 2017/11/3.
//  Copyright © 2017年 DeLongYang. All rights reserved.
//

#import "DLCircleListHeaderView.h"
#import "DLCircleListHeaderViewModel.h"    // 引入 VM
#import "DLCircleListCollectionViewCell.h"  // 引入cell

// 头部的
@interface DLCircleListHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)DLCircleListHeaderViewModel *viewModel;

@property (nonatomic,strong)UIView *bgView;

@property (nonatomic,strong)UILabel *titleLable;

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@end

@implementation DLCircleListHeaderView



#pragma mark ---- System UpdateConstraints
- (void)updateConstraints
{
    WS(weakSelf)
    CGFloat paddingEdge = 10.0;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf);
        make.bottom.equalTo(@(-paddingEdge));
    }];
    
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.equalTo(@(paddingEdge));
//        make.right.equalTo(@(-paddingEdge));
//        make.height.equalTo(@20);
        
//        make.left.equalTo(@(paddingEdge));
//        make.top.equalTo(@(paddingEdge));
//        make.right.equalTo(@(-paddingEdge));
//        make.height.equalTo(@20);
        
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.right.equalTo(@(10));
        make.height.equalTo(@20);

    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(weakSelf);
        make.bottom.equalTo(weakSelf).offset(-paddingEdge);
        make.top.equalTo(weakSelf.titleLable.mas_bottom).offset(paddingEdge);
    }];
    [super updateConstraints];
}

#pragma mark ---- DLViewProtocol

- (instancetype)initWithViewModel:(id<DLViewModelProtocol>)viewModel
{
    // 注意 父类中是没有
    self.viewModel = (DLCircleListHeaderViewModel *)viewModel;
    return  [super initWithViewModel:viewModel];
}

- (void)dl_setupViews
{
    [self addSubview:self.bgView];
    [self addSubview:self.titleLable];
    [self addSubview:self.collectionView];
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
}

- (void)dl_bindViewModel
{
    @weakify(self)
    [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        [self.collectionView reloadData];
    }];
    
    RAC(self.titleLable,text) = [[RACObserve(self, viewModel.title) distinctUntilChanged] takeUntil:self.rac_willDeallocSignal];
    
}

#pragma mark ----  Lazy load getter Method
- (DLCircleListHeaderViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[DLCircleListHeaderViewModel alloc] init];
    }
    return _viewModel;
}

- (UIView *)bgView
{
    if (!_bgView) {
        
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = white_color;
    }
    
    return _bgView;
}

- (UILabel *)titleLable
{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]init];
        _titleLable.textColor = MAIN_BLACK_TEXT_COLOR;
        _titleLable.font = SYSTEMFONT(15);
    }
    return _titleLable;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.collectionViewLayout = self.flowLayout;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = white_color;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[DLCircleListCollectionViewCell class] forCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([DLCircleListCollectionViewCell class])]];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)flowLayout
{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout.minimumLineSpacing = 10;  
        _flowLayout.minimumInteritemSpacing = 10;
    }
    return _flowLayout;
}

#pragma mark ---- UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel.cellClickSubject sendNext:nil];
}

#pragma mark ---- UICollectionViewDataSource       UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.viewModel.dataArray.count+1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DLCircleListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([DLCircleListCollectionViewCell class])] forIndexPath:indexPath];
    if (self.viewModel.dataArray.count>indexPath.row) {
        cell.viewModel = self.viewModel.dataArray[indexPath.row];
    }
    
    if (self.viewModel.dataArray.count == indexPath.row) {
        cell.type = @"加入新圈子";
    }
    
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80, 105);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(0, 10, 0, 10);
}











































@end
